package com.maochang.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.*;
import com.maochang.pojo.*;
import com.maochang.pojo.util.DataTree1;
import com.maochang.pojo.util.RT;
import com.maochang.pojo.util.ResultDT;
import com.maochang.service.XunJianRenWuZhiXingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service("XunJianRenWuZhiXingServicelmpl")
public class XunJianRenWuZhiXingServiceImpl extends ServiceImpl<PsInspectiontaskMainMapper, PsInspectiontaskMain> implements XunJianRenWuZhiXingService {

    @Resource
    private   PsInspectiontaskMainMapper mainMapper;
    @Resource
    private PsSysTemConfigMapper psSysTemConfigMapper;
    @Resource
    private PsLineInfoMapper psLineInfoMapper;
    @Resource
    private PsPoleInfoMapper psPoleInfoMapper;
    @Resource
    private PsInspectiontaskDetailMapper psInspectiontaskDetailMapper;
    @Resource
    private  PsInspectorDetailMapper psInspectorDetailMapper;
    @Resource
    private  PsUserMapper psUserMapper;
    @Override
    public Page<PsInspectiontaskMain> ChaXjZhiXing(PsInspectiontaskMain ps,String kai,String jie, int page, int size,HttpSession httpSession) {
        //1.表格初始化加载
        //2.表格带参数    执行中  或者   默认查询的是已分配
        PsUser yongHu = (PsUser) httpSession.getAttribute("loginUser");
        Page<PsInspectiontaskMain> psliPage=new Page<>(page,size);
        List<Integer>   shuZi=null;
        //主表id -- 查询 巡检员明细表
        QueryWrapper<PsInspectorDetail> wrapper2= Wrappers.query();
        wrapper2.eq("inspectorName",yongHu.getUserName());
        List<PsInspectorDetail> list= psInspectorDetailMapper.selectList(wrapper2);
         shuZi=new ArrayList<>();
        for (PsInspectorDetail aa:list){
               //查询出来子表有的
               shuZi.add(aa.getTaskId());
        }
        //主表信息
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        //如果状态为空证明   不是带参数查询
        if(ps.getTaskStatus()==null){
            //五参数默认查询
            //System.out.println("长度"+shuZi.size()+"-============================");
            if(shuZi.size()>0){
                //System.out.println("======================有值1=================");
                wrapper.like(!StringUtils.isBlank(ps.getInspectionTaskCode()),"inspectionTaskCode",ps.getInspectionTaskCode())
                        .like(!StringUtils.isBlank(ps.getLineCode()),"lineCode",ps.getLineCode())
                        .like(!StringUtils.isBlank(ps.getIssuedByName()),"issuedByName",ps.getIssuedByName())
                        .ne("taskStatus","待分配")
                        .ge(!StringUtils.isBlank(kai),"issuedTime",kai)
                        .le(!StringUtils.isBlank(jie),"issuedTime",jie);
                        wrapper.in("id",shuZi);
            }else {
                    wrapper.in("id", 0);
            }
        }else {
            //如果不为空 证明带参数
            //System.out.println("长度"+shuZi.size()+"-============================");
            if(shuZi.size()>0){
                //System.out.println("======================有值2=================");
                //有参数查询寻
                wrapper.like(!StringUtils.isBlank(ps.getInspectionTaskCode()), "inspectionTaskCode", ps.getInspectionTaskCode())
                        .like(!StringUtils.isBlank(ps.getLineCode()), "lineCode", ps.getLineCode())
                        .like(!StringUtils.isBlank(ps.getIssuedByName()), "issuedByName", ps.getIssuedByName())
                        .eq("taskStatus", ps.getTaskStatus())
                        .ge(!StringUtils.isBlank(kai), "issuedTime", kai)
                        .le(!StringUtils.isBlank(jie), "issuedTime", jie);
                wrapper.in("id", shuZi);
            }else {
                wrapper.in("id", 0);
            }
        }
       //查询  当前巡检员  需要回执的  的信息
        return mainMapper.AllPXjList(psliPage,wrapper);
    }
    /**
     * 类型数据
     * @return
     */
    @Override
    public List<PsSystemconfig> ChaXia() {
        QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
        wrapper.eq("parentId",13).orderByAsc("id");
        List<PsSystemconfig>  psList =psSysTemConfigMapper.selectList(wrapper);
        return psList;
    }
    /**
     * 树形控件的数据
     * @param lineName
     * @return
     */
    @Override
    public  ResultDT ChaXina(String lineName) {
        ResultDT resultDT=new ResultDT();
        DataTree1 tree1=new DataTree1();
        //===================================================
        QueryWrapper<PsLineInfo> wrapper= Wrappers.query();
        wrapper.eq(!StringUtils.isBlank(lineName),"lineName",lineName);
        QueryWrapper<PsPoleInfo> wrapperr= Wrappers.query();
        wrapperr.eq(!StringUtils.isBlank(lineName),"lineName",lineName);
        PsLineInfo psLineInfo=psLineInfoMapper.selectOne(wrapper);
        List<PsPoleInfo> psPoleInfos=psPoleInfoMapper.selectList(wrapperr);
        //=======================================================
        tree1.setId(String.valueOf(psLineInfo.getId()));
        tree1.setTitle(psLineInfo.getLineName());
        tree1.setParentId("0");
        //=============================================================
        List<DataTree1> children=new ArrayList<>();
        for (PsPoleInfo psPoleInfo:psPoleInfos) {
            DataTree1 tree2=new DataTree1();
            tree2.setId("0"+psPoleInfo.getId());
            tree2.setTitle(psPoleInfo.getPoleCode());
            tree2.setParentId(String.valueOf(tree1.getId()));
            tree2.setChildren(null);
            if(tree2.getChildren()==null){
                tree2.setLast(Boolean.parseBoolean("true"));
                tree2.setLevel("2");
            }
            children.add(tree2);
        }
        tree1.setChildren(children);
        if(children.size()!=0){
            tree1.setLast(Boolean.parseBoolean("false"));
            tree1.setLevel("1");
        }
        List<DataTree1> aa= new ArrayList<>();
        aa.add(tree1);
        //================================================================
        RT at=new RT();
        at.setCode(200);
        at.setMessage("操作成功");
        resultDT.setStatus(at);
        resultDT.setData(aa);
        return resultDT;
    }

    @Override
    public PsPoleInfo ChaLu(PsPoleInfo poleCode) {
        QueryWrapper<PsPoleInfo> wrapper= Wrappers.query();
        wrapper.eq("poleCode",poleCode.getPoleCode());
        PsPoleInfo  psList =psPoleInfoMapper.selectOne(wrapper);
        return psList;
    }

    @Override
    public List<PsSystemconfig> ChaQueLei() {
        QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
        wrapper.eq("parentId",1).orderByAsc("id");
        List<PsSystemconfig>  psList =psSysTemConfigMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public List<PsSystemconfig> ChaQueJi() {
        QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
        wrapper.eq("parentId",11).orderByAsc("id");
        List<PsSystemconfig>  psList =psSysTemConfigMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public String addPsIt(PsInspectiontaskDetail psInspectiontaskDetail) {
        int aa= 0;
        //先查询吃 此任务此
           //1.拿到主任务id
           //2.拿到 线路编号
           //3.拿到 完成状态
           //4.杆塔编号
        QueryWrapper<PsInspectiontaskDetail> csj= Wrappers.query();
        csj.eq("taskId",psInspectiontaskDetail.getTaskId());
        csj.eq("lineCode",psInspectiontaskDetail.getLineCode());
        csj.eq("taskStatus","34");
        csj.eq("poleCode",psInspectiontaskDetail.getPoleCode());
        int pa=  psInspectiontaskDetailMapper.selectCount(csj);
        if(pa>0){
            System.out.println("走这个了");
            return  "此信息已经被录入";
        }else {
            System.out.println("我也走这个了");
            //任务状态id  已完成
            psInspectiontaskDetail.setTaskStatus(34);
            //杆台id 这个不是个死值 根据 code  查询iD
            QueryWrapper<PsPoleInfo> gtj= Wrappers.query();
            gtj.eq("poleCode",psInspectiontaskDetail.getPoleCode());
            PsPoleInfo ps= psPoleInfoMapper.selectOne(gtj);
            psInspectiontaskDetail.setPoleId(ps.getId());
            //杆台 code前端拿到了
            //====================
            //他录入了 就肯定是  bug
            psInspectiontaskDetail.setIsBug(1);
            //完好率前台有 人机录入了
            //=====================
            //缺陷级别id  前台赋值
            //=====================
            //缺陷级别名称
            QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
            wrapper.eq("id",psInspectiontaskDetail.getBugLevel());
            PsSystemconfig Sc = psSysTemConfigMapper.selectOne(wrapper);
            psInspectiontaskDetail.setBugLevelName(Sc.getConfigName());
            //缺陷类型id  前台赋值
            //=======================
            QueryWrapper<PsSystemconfig> wrapper1= Wrappers.query();
            wrapper1.eq("id",psInspectiontaskDetail.getBugType());
            PsSystemconfig Sc1 = psSysTemConfigMapper.selectOne(wrapper1);
            //前端只有 缺陷类型编号 还得查 缺陷级别 name
            psInspectiontaskDetail.setBugTypeName(Sc1.getConfigName());
            //前端只有 发现人name
            //======================
            //巡检人  也就是 发现人 code   得查一下
            QueryWrapper<PsUser> wrapper2= Wrappers.query();
            wrapper2.eq("userName",psInspectiontaskDetail.getDiscovererName());
            PsUser UserCha = psUserMapper.selectOne(wrapper2);
            psInspectiontaskDetail.setDiscovererCode(UserCha.getUserCode());
            //发现时间 前端有
            //=======================
            //isSolve  1 消缺了   未消缺
            psInspectiontaskDetail.setIsSolve(0);
            //isbugLevel  是否确认缺陷级别 肯定是没有  死值
            psInspectiontaskDetail.setIsBugLevel(0);
            //巡检时间
            psInspectiontaskDetail.setInspectTime(LocalDate.now());
            //创建时间前端有
            //====================================
            //创建者 当前登录人  //我拿的是前端的会话
            psInspectiontaskDetail.setCreateBy(psInspectiontaskDetail.getDiscovererName());
            //最后啊修改时间  当前时间
            psInspectiontaskDetail.setLastUpdateTime(LocalDateTime.now());
        }
        aa= psInspectiontaskDetailMapper.insert(psInspectiontaskDetail);
        return aa>0?"插入信息成功":"信息插入失败";
    }

    @Override
    public boolean SiZhiXiu(PsInspectiontaskMain id) {
        boolean a;
        QueryWrapper<PsInspectiontaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",id.getId());
        if(mainMapper.SiZhiXiu(id)){
            a=true;
        }else {
            a=false;
        }
        return a;
    }

    @Override
    public PsInspectiontaskDetail ChaKanLu(PsInspectiontaskDetail psInspectiontaskDetail) {
        QueryWrapper<PsInspectiontaskDetail> wrapper= Wrappers.query();
        wrapper.eq("poleCode",psInspectiontaskDetail.getPoleCode())
                .eq("taskId",psInspectiontaskDetail.getTaskId());
        PsInspectiontaskDetail  psList =psInspectiontaskDetailMapper.selectOne(wrapper);
        return psList;
    }

    @Override
    public boolean ZhiSiZhiXiu(PsInspectiontaskMain id) {
        boolean a;
        QueryWrapper<PsInspectiontaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",id.getId());
        if(mainMapper.ZhiSiZhiXiu(id)){
            a=true;
        }else {
            a=false;
        }
        return a;
    }

    @Override
    public boolean ShiJianSiZhiXiu(PsInspectiontaskMain id) {
        boolean a;
        QueryWrapper<PsInspectiontaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",id.getId());
        if(mainMapper.ShiJianSiZhiXiu(id)){
            a=true;
        }else {
            a=false;
        }
        return a;
    }


    @Override
    public boolean CaoZuoXiuGai(PsInspectiontaskDetail psInspectiontaskDetail) {
        QueryWrapper<PsInspectiontaskDetail> wrapper=Wrappers.query();
        wrapper.eq("taskId",psInspectiontaskDetail.getTaskId())
                .eq("poleCode",psInspectiontaskDetail.getPoleCode());
        int s=psInspectiontaskDetailMapper.update(psInspectiontaskDetail,wrapper);
        boolean fAlg=false;
        if (s>0){
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public Page<PsInspectiontaskDetail> QueXianCha(PsInspectiontaskDetail ps,String kai,String jie,String kai1,String jie1, int page, int size) {
        Page<PsInspectiontaskMain> psliPage=new Page<>(page,size);
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        wrapper.eq("TaskStatus",34)
                .eq(ps.getTaskId()!=null,"taskId",ps.getTaskId())
                .like(!StringUtils.isBlank(ps.getLineCode()),"lineCode",ps.getLineCode())
                .eq(ps.getBugLevel()!=null,"bugLevel",ps.getBugLevel())
                .eq(ps.getBugType()!=null,"bugType",ps.getBugType())
                .ge(!StringUtils.isBlank(kai),"discoverTime",kai)
                .le(!StringUtils.isBlank(jie),"discoverTime",jie)
                .ge(!StringUtils.isBlank(kai1),"creationTime",kai1)
                .le(!StringUtils.isBlank(jie1),"creationTime",jie1);
        return psInspectiontaskDetailMapper.AllPXjList(psliPage,wrapper);
    }
    @Override
    public Page<PsInspectiontaskDetail> QueXianCha2(PsInspectiontaskDetail ps, String kai, String jie, String kai1, String jie1, int page, int size, HttpSession session) {
        Page<PsInspectiontaskMain> psliPage=new Page<>(page,size);
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        //创建者
        PsUser yongHu = (PsUser) session.getAttribute("loginUser");
                     wrapper.eq("TaskStatus",34)
                    .eq(ps.getTaskId()!=null,"taskId",ps.getTaskId())
                    .like(!StringUtils.isBlank(ps.getLineCode()),"lineCode",ps.getLineCode())
                    .eq(ps.getBugLevel()!=null,"bugLevel",ps.getBugLevel())
                    .eq(ps.getBugType()!=null,"bugType",ps.getBugType())
                    .ge(!StringUtils.isBlank(kai),"discoverTime",kai)
                    .le(!StringUtils.isBlank(jie),"discoverTime",jie)
                    .ge(!StringUtils.isBlank(kai1),"creationTime",kai1)
                    .le(!StringUtils.isBlank(jie1),"creationTime",jie1)
                    .eq("isSolve",0).eq("isBug","1").eq("createBy",yongHu.getUserName());

        return psInspectiontaskDetailMapper.AllPXjList(psliPage,wrapper);
    }

}
